package org.thoneloe.banner;

import com.github.lalyos.jfiglet.FigletFont;
import com.github.lalyos.jfiglet.JFiglet;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.ansi.AnsiColor;
import org.springframework.boot.ansi.AnsiOutput;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.thoneloe.constant.BannerConstant;
import org.thoneloe.properties.BannerProperties;

import javax.print.attribute.standard.MediaSize;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * @Author: Reverie
 * @CreateTime: 2025-05-16
 */
@RequiredArgsConstructor
public class ThoneloeBanner implements InitializingBean {
    private final BannerProperties bannerProperties;
    private static final String NAME = " :: %s Boot App :: ";
    private static final String VERSION = BannerConstant.VERSION;

    public void printBanner() {
        try {
            InputStream inputStream = ThoneloeBanner.class.getResourceAsStream("/fonts/" + bannerProperties.getFont().name().toLowerCase() + ".flf");
            String printBannerName = FigletFont.convertOneLine(inputStream, bannerProperties.getBannerName());
            inputStream.close();
            System.out.println(AnsiOutput.toString(AnsiColor.BLUE, printBannerName, "\n", AnsiColor.RED, String.format(NAME, bannerProperties.getBannerName()), " ", AnsiColor.RED, VERSION));
        } catch (IOException e) {
            System.out.println("banner print fail");
        }
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        printBanner();
    }
}
